class RequestsController < ApplicationController
  # GET /requests
  # GET /requests.xml
  def index
    @requests = Request.all

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @requests }
    end
  end

  # GET /requests/1
  # GET /requests/1.xml
  def show
    @request = Request.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @request }
    end
  end

  # GET /requests/new
  # GET /requests/new.xml
  def new
    @request = Request.new    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @request }
    end
  end

  # GET /requests/1/edit
  def edit
    @request = Request.find(params[:id])
  end

  # POST /requests
  # POST /requests.xml
  def create    
    @request = Request.new(params[:request])
    
    #@request.service_id = params[:service_id]
    respond_to do |format|
      if @request.save
        format.html { redirect_to(@request, :notice => 'Request was successfully created.') }
        format.xml  { render :xml => @request, :status => :created, :location => @request }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @request.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /requests/1
  # PUT /requests/1.xml
  def update
    @request = Request.find(params[:id])
    respond_to do |format|
      if @request.update_attributes(params[:request])
        format.html { redirect_to(@request, :notice => 'Request was successfully updated.') }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @request.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /requests/1
  # DELETE /requests/1.xml
  def destroy
    @request = Request.find(params[:id])
    @request.destroy

    respond_to do |format|
      format.html { redirect_to(requests_url) }
      format.xml  { head :ok }
    end
  end
  
  def destroy_return
    @request = Request.find(params[:id])
    @request.destroy

    respond_to do |format|
      format.html { redirect_to(back) }
      format.xml  { head :ok }
    end
  end
  
  def select_room
    @room = Room.new
    @services = Service.all
    @request = Request.new
  end
  
  def verify_room         
    @acc = Accomodation.find_by_room_id(params[:num])    
      
    respond_to do |format|
      if @acc.nil?
        flash[:notice] = 'Theres no accomodation registered to that room. Unable to register service request.'
        format.html { redirect_to(:action => 'select_room' ) }        
      else
        flash[:notice] = 'Valid room found.'     
        if @acc.checkedOut==true
           flash[:notice] = 'The accomodation has already been checked out.'
           format.html { redirect_to(:action => 'select_room' ) }        
        else        
          cookies[:acc_id] = {:value => @acc.id, :expires => 31.days.from_now }
          format.html { redirect_to(:action => 'verified_room' ) }
          format.html { render :action => "new" }
          format.xml  { render :xml => @request.errors, :status => :unprocessable_entity }
        end
      end        
    end
  end
  
  def verified_room
    @services = Service.all
    @request = Request.new
    @acc = Accomodation.find_by_room_id(params[:num])
    @cookie_data = cookies[:acc_id]
    cookies.delete :acc_id
  end
end
